/*** 更新订单**/
faApp.controller('UpdateOrderCtrl',['$scope','updateOrderService','$location','$controller','$routeParams','$filter','$route',function($scope,updateOrderService,$location,$controller,$routeParams,$filter,$route) {   
  $controller('BaseListCtrl', {$scope: $scope}); 

  $scope.param={};
  $scope.param.orderId=$routeParams.orderId;
  $scope.orderFlowList={};//订单追踪时间
  $scope.orderDateInfo={};//订单时间
  $scope.item={};//订单基本信息
  //待提交参数
  $scope.orderFlow=[];//跟踪时间
  $scope.orderFlowId=[];//跟踪时间
  $scope.orderFlowEnd=[];//跟踪时间后缀
  $scope.dateparam={};//时间

  updateOrderService.queryOrderDetail($scope.param).then(function(json) {
     if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
         $scope.item=json.orderVo;
         $scope.orderDateInfo=json.orderInfo;
         $scope.orderFlowList=json.orderFlowList;
         //时间格式处理
         angular.forEach($scope.orderFlowList,function(o,i){
          $scope.orderFlow[i]=$filter("date")(o.flowDate,"yyyy-MM-dd");
          $scope.orderFlowEnd[i]=$filter("limitTo")(o.flowDateStr,-8);
         });
         //订单时间   后缀
         $scope.dateparam.orderCreateDate=$filter("limitTo")($scope.orderDateInfo.orderCreateDate,10);
         $scope.dateparam.orderCreateDateEnd=$filter("limitTo")($scope.orderDateInfo.orderCreateDate,-8);
        //用户认证时间   后缀
         $scope.dateparam.userAuthDate=$filter("limitTo")($scope.orderDateInfo.userAuthDate,10);
         $scope.dateparam.userAuthDateEnd=$filter("limitTo")($scope.orderDateInfo.userAuthDate,-8);
        //用户注册时间   后缀
         $scope.dateparam.userCreateDate=$filter("limitTo")($scope.orderDateInfo.userCreateDate,10);
         $scope.dateparam.userCreateDateEnd=$filter("limitTo")($scope.orderDateInfo.userCreateDate,-8);
      }
  });

  $scope.save = function(){
    if (!$scope.form.$valid) {
      alert("请先完整填写表单");
      return;
    }
    
    if($scope.dateparam.userAuthDate){
      if($scope.dateparam.userAuthDateEnd){
         $scope.dateparam.userAuthDate+=' ' +$scope.dateparam.userAuthDateEnd;
      }else{
        alert("请完整填写认证时间");
        return;
      }
    }else if($scope.dateparam.userAuthDateEnd){
      if($scope.dateparam.userAuthDate){
         $scope.dateparam.userAuthDate+=' ' +$scope.dateparam.userAuthDateEnd;
      }else{
        alert("请完整填写认证时间");
        return;
      }
    }
    //注册时间是否小于订单预约时间
    var uCreateDate=$scope.dateparam.userCreateDate+' ' +$scope.dateparam.userCreateDateEnd;
    var oCreateDate=$scope.dateparam.orderCreateDate+' ' +$scope.dateparam.orderCreateDateEnd;
    console.log(uCreateDate+"="+oCreateDate);
    if(uCreateDate>oCreateDate){
      alert("注册时间必须小于订单预约时间");
      return ;
    }
    $scope.orderFlowParam=[];
    //拼接跟踪时间
    for (k in $scope.orderFlow){
      $scope.orderFlowParam[k] =$scope.orderFlow[k] +' ' + $scope.orderFlowEnd[k];
      if(k>0){
        if($scope.orderFlowParam[k-1]>$scope.orderFlowParam[k]){
          alert("订单追踪时间存在错误!");
          return ;
        }
      }
    }

    delete $scope.dateparam.userAuthDateEnd;
    //拼时间
    $scope.dateparam.userCreateDate+=' ' +$scope.dateparam.userCreateDateEnd;
    delete $scope.dateparam.userCreateDateEnd;
    $scope.dateparam.orderCreateDate+=' ' +$scope.dateparam.orderCreateDateEnd;
    delete $scope.dateparam.orderCreateDateEnd;

    var p={
      'orderId':$scope.param.orderId,
      'orderFlowId':$scope.orderFlowId.join(","),
      'orderFlow':$scope.orderFlowParam.join(","),
      'dateparam':angular.toJson($scope.dateparam)
    };  

    updateOrderService.updateOrderDetail(p).then(function(json) {
       if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("更新成功");
          $route.reload();
        }
    });
  }
  //2017 节假日
  var holiday="2017-10-01,2017-10-02,2017-10-03,2017-10-04,2017-10-05,2017-10-06,2017-10-07,2017-10-08";

  /*** 自动生成 时分秒   ***/
  $scope.upDate=function(v){
    if(v!='220'){
      return ;
    }

    var dksj;// 已签约打款  基础时间
    for (k in $scope.orderFlowList){
      var o=$scope.orderFlowList[k];
      if(o.oStatus=='220'){
        dksj=$scope.orderFlow[k];
        var a=$scope.orderFlowEnd[k];
        //是否正常时间
        if(a==undefined||!/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/.test(a)
         ||dksj==undefined||!/^20\d{2}-\d{2}-\d{2}$/.test(dksj)
         ){
          return ;
        }
        //是否大于当前时间
         var date=new Date(dksj+" "+a);
        if(date>=new Date()){
          alert("签约时间不能大于当前时间!");
          return;
        }
        break;
      }
    }
    for (k in $scope.orderFlowList) {
      var o=$scope.orderFlowList[k];
      var status = parseInt(o.oStatus);
      if(status==110){//提交预约
        var endDay=daysJian(dksj,-daysRandom(3,5));
        $scope.orderFlow[k]=$filter("date")(endDay,"yyyy-MM-dd");
        //预约时间
        $scope.dateparam.orderCreateDate=$scope.orderFlow[k];

        console.log("预约时间:"+endDay);
      }else if(status<=130){//预约成功 中间状态 最多8小时
        var endDay=$scope.orderFlow[k-1];
        var endDayHour=$scope.orderFlowEnd[k-1];
        var s = addRandomHours(endDay+" "+endDayHour,1,4);
        $scope.orderFlow[k]=$filter("date")(s,"yyyy-MM-dd");
        $scope.orderFlowEnd[k]=$filter("date")(s,"HH:mm:ss");
      }else if(status==220){//基准时间  已签约打款 不动

      }else if(status>220&&status<=240){//确认打款  1个工作日内
        var endDay=$scope.orderFlow[k-1];
        var endDayHour=$scope.orderFlowEnd[k-1];
        var s = addRandomHours(endDay+" "+endDayHour,2,12);
        $scope.orderFlow[k]=$filter("date")(s,"yyyy-MM-dd");
        $scope.orderFlowEnd[k]=$filter("date")(s,"HH:mm:ss");
      }
      else if(status>240&&status<320){//15个工作日
        var endDay=$scope.orderFlow[k-1];
        var endDayHour=$scope.orderFlowEnd[k-1];
        var s = addRandomHours(endDay+" "+endDayHour,5,24);
        $scope.orderFlow[k]=$filter("date")(s,"yyyy-MM-dd");
        $scope.orderFlowEnd[k]=$filter("date")(s,"HH:mm:ss");
      }else if(o.oStatus==320){
        var endDay=$scope.orderFlow[k-1];
        var endDayHour=$scope.orderFlowEnd[k-1];
        var s = addRandomHours(endDay+" "+endDayHour,1,5);
        $scope.orderFlow[k]=$filter("date")(s,"yyyy-MM-dd");
        $scope.orderFlowEnd[k]=$filter("date")(s,"HH:mm:ss");
      }else{
        var endDay=$scope.orderFlow[k-1];
        var endDayHour=$scope.orderFlowEnd[k-1];
        var s = addRandomHours(endDay+" "+endDayHour,2,10);
        $scope.orderFlow[k]=$filter("date")(s,"yyyy-MM-dd");
        $scope.orderFlowEnd[k]=$filter("date")(s,"HH:mm:ss");
      }

    };
  }

  /** 时间  设置天数 -1 天 **/
  function daysJian(d,day){
   //判断工作日内加
   var date=new Date(d);
   //循环加1   工作日
   for (var i = 1; i <= Math.abs(day); i++) {
     date.setDate(date.getDate()+(day>0?1:-1));
     var dstr = $filter("date")(date,"yyyy-MM-dd");
     if(date.getDay()==0||date.getDay()==6||holiday.indexOf(dstr)>=0){
        i--;
     }
   };
   console.log("add day end="+$filter("date")(date,"yyyy-MM-dd"));
   return date;
  }  
    /** 时间  设置小时 随机分钟 **/
  function addRandomHours(d,min,max){
   //判断工作日内加
   var date=new Date(d);
   var addHours=date.getHours()+daysRandom(min,max);
   console.log("add addRandomHours ="+$filter("date")(date,"yyyy-MM-dd"));
   if(addHours>20){//小时超过晚上8点 天数+1
      date = daysJian(date,1);
      date.setHours(daysRandom(9,12));
      console.log("add addRandomHours>20 ="+$filter("date")(date,"yyyy-MM-dd"));
   }else{
    date.setHours(addHours);
    var dstr=$filter("date")(date,"yyyy-MM-dd");
     //判断当前是否工作日
    if(date.getDay()==0||date.getDay()==6||holiday.indexOf(dstr)>=0){
      date=daysJian(date,1);
      date.setHours(daysRandom(9,20));
    }
   }
   date.setMinutes(daysRandom(1,59));
   date.setSeconds(daysRandom(1,59));
 
   return date;
  }  
      /** 时间  随机工作时分秒    **/
  function randomWorkHousr(d){
   var date=new Date(d);
   date.setHours(daysRandom(9,20));  
   date.setMinutes(daysRandom(59,59));  
   date.setSeconds(daysRandom(59,59));  
   return date;
  } 
   /** 时间  随机中间数 **/
  function daysRandom(min,max){
    var day=Math.random()*(max-min)+min;
    return day;
  }  

}]);
/*** 更新产品**/
faApp.controller('UpdateProductDateCtrl',['$scope','updateOrderService','productService','$location','$controller','$routeParams','$filter','$route',function($scope,updateOrderService,productService,$location,$controller,$routeParams,$filter,$route) {   
  $controller('BaseListCtrl', {$scope: $scope}); 
  
    $scope.param={};
    $scope.param.pId=$routeParams.pId;
    $scope.dateparam={};
     $scope.dateparam.pId= $scope.param.pId;
  //如果有值，则说明是点了复制按钮，则根据ID加载产品
    updateOrderService.queryOrderDetail($scope.param).then(function(json){
      $scope.project = json.projectVo;
      //开始销售时间   
      if(json.projectVo.pSaleTimeStart)
      $scope.dateparam.saleTimeStart=$filter("date")(json.projectVo.pSaleTimeStart,"yyyy-MM-dd");
      //结束销售时间   
      if(json.projectVo.pSaleTimeEnd)
        $scope.dateparam.saleTimeEnd=$filter("date")(json.projectVo.pSaleTimeEnd,"yyyy-MM-dd");

      $scope.minOrderCreateDate=json.minOrderCreateDate;
      $scope.maxOrderCreateDate=json.maxOrderCreateDate;

    });

  $scope.save = function(){
    if (!$scope.form.$valid) {
      alert("请先完整填写表单");
      return;
    }
    if($scope.dateparam.saleTimeEnd){
       //开始销售时间必须小于结束销售时间
      if($scope.dateparam.saleTimeStart>$scope.dateparam.saleTimeEnd){
        alert("注册时间必须小于订单预约时间");
        return ;
      }
    }
    var p={
      'pId':$scope.pid,
      'dateparam':angular.toJson($scope.dateparam)
    };  
    updateOrderService.updateOrderDetail(p).then(function(json) {
       if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("更新成功");
          $route.reload();
        }
    });
  }

}]);

/*** 新增可修改订单**/
faApp.controller('UpdateOrderAddCtrl',['$scope','updateOrderService','$location','$controller','$routeParams','$filter','$route',function($scope,updateOrderService,$location,$controller,$routeParams,$filter,$route) {   
  $controller('BaseListCtrl', {$scope: $scope}); 
  $scope.save = function(){
   if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else{
      updateOrderService.saveOrderIdList({"idList":$scope.idList}).then(function(json){
      if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          alert("新增成功");
          $route.reload();
        }
    });

    }
  }  
}]);
/*** 更新订单搜索 **/
faApp.controller('UpdateOrderListCtrl',['$scope','updateOrderService','$location','$controller','$routeParams','$filter',function($scope,updateOrderService,$location,$controller,$routeParams,$filter) {   
  $controller('BaseListCtrl', {$scope: $scope}); 


  $scope.list = [];
  $scope.loading = true;
  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1
  };
  $scope.$watch('config',function(){
    if ($scope.watchIsChange()){
       if($scope.config.orderIds!=undefined){
        console.log("query idList");
         updateOrderService.getOrderIdList($scope.config).then(function(json){
          if (json.statusCode){
              alert("出错了:" + json.statusCode);
          }else{
           $scope.orderList=json.orderList;

          }
        });
      }
     
    }
  },true)
   
}]);  